package com.system

import grails.async.Promises

import grails.converters.JSON
import java.sql.Connection
import java.sql.Statement

class UserController {

    def dataSource

    def index() {
        render User.list() as JSON
    }

    def db() {
        System.out.println(dataSource);
        Connection conn = dataSource.getConnection();
        Statement stat = conn.createStatement();
        stat.execute("select version()");
        stat.execute("select * from user where id = 1");
        conn.close();
        render "done."
    }

    def batch() {
        def promise = Promises.task {
            User user
            def u = User.withNewSession { session ->
                (0..2000).each { i ->
                    user = new User(UUID.randomUUID().toString(), "123456", "1123456${i}@qq.com")
                    user.save()
                    if (i.mod(1000) == 0) {
                        session.flush()
                        session.clear()
                    }
                }
            }
            u
        }

        println promise
        def user1 = promise.get()

        render user1 as JSON
    }
}
